package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.model.SortMarker;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.InterestDetailForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.InterestDetailVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.CloseStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.TopStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.UpvoteFlag;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.UpvoteStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.UserEntityVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.XinanPushForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.UsersQueryVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.ViewPointBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.result.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.vo.*;
import com.cloudbroker.bcs.platform.ltb.middle.entity.*;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.lightinvesting.LightinvestingService;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.usercenter.UserCenterService;
import com.cloudbroker.bcs.platform.ltb.middle.model.UserCache;
import com.cloudbroker.bcs.platform.ltb.middle.service.*;
import com.cloudbroker.bcs.platform.ltb.middle.util.UpvoteCacheUtil;
import com.cloudbroker.bcs.platform.ltb.middle.util.UserTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by world on 16/7/11.
 */

@Service
public class ViewpointBizServiceImpl implements ViewPointBizService {

    //观点信息表的操作
    @Resource
    private ViewpointMsgService viewpointMsgService;

    @Resource
    private ViewpointRecomService viewpointRecomService;

    @Resource
    private ViewpointCombinationService viewpointCombinationService;

    @Resource
    private UpvoteRecordService upvoteRecordService;

    @Resource
    private CombineService combineService;

    @Resource
    private UserService userService;

    @Resource
    private ViewpointTagService viewpointTagService;

    @Resource
    private ViewpointTopicService viewpointTopicService;

    @Resource
    private LightinvestingService lightinvestingService;

    @Resource
    private RecommendRuleService recommendRuleService;

    @Resource
    private UserCenterService userCenterService;
    @Resource
    private PushService pushService;

    @Override
    @Transactional
    public ViewpointSaveResult funcSaveViewpoint(ViewpointSaveForm viewpointSaveForm){
        //粉丝禁止发表观点
        if(StringUtils.isEmpty(viewpointSaveForm.getUser().getUser_type()) ||
                Integer.valueOf(viewpointSaveForm.getUser().getUser_type()) != UserType.LEADER.getVal()){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_CREATE_NOT_AUTH,ViewpointSaveResult.class);
        }
        ViewpointMsg viewpointMsg = new ViewpointMsg();
        if (viewpointSaveForm.getViewpoint_type()!=null && "2".equals(viewpointSaveForm.getViewpoint_type())){
            viewpointMsg.setViewpoint_type("2");
            viewpointMsg.setVoice_path(viewpointSaveForm.getVoice_path());
        }else{
            viewpointMsg.setViewpoint_type("1");
            if (StringUtil.isBlank(viewpointSaveForm.getViewpoint_theme()) ||
                    StringUtil.isBlank(viewpointSaveForm.getViewpoint_content())){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PARAM_ERROR,"观点标题和观点内容不能为空",ViewpointSaveResult.class);
            }
        }
        String en_combi_code = viewpointSaveForm.getEn_combi_code();
        String viewpoint_theme = viewpointSaveForm.getViewpoint_theme();
        String viewpoint_content = viewpointSaveForm.getViewpoint_content();
        String file_path = viewpointSaveForm.getFile_path();
        //getUser_id
        Long user_id = viewpointSaveForm.getUser().getUser_id();
        String company_id = viewpointSaveForm.getCompany_id();

        Long viewpoint_tag_id = "".equals(viewpointSaveForm.getViewpoint_tag_id())?
                null:Long.valueOf(viewpointSaveForm.getViewpoint_tag_id());
        Long viewpoint_topic_id = "".equals(viewpointSaveForm.getViewpoint_topic_id())?
                null:Long.valueOf(viewpointSaveForm.getViewpoint_topic_id());
        //组装
        viewpointMsg.setUser_id(user_id);
        viewpointMsg.setCompany_id(company_id);
        viewpointMsg.setViewpoint_content(viewpoint_content);
        viewpointMsg.setViewpoint_image_keys(file_path);
        viewpointMsg.setViewpoint_theme(viewpoint_theme);
        viewpointMsg.setUpdate_time(System.currentTimeMillis());
        viewpointMsg.setSend_time(System.currentTimeMillis());
        viewpointMsg.setIs_top(TopStatus.NOT_TOP.getVal());
        viewpointMsg.setShare_num(0);
        viewpointMsg.setUpvote_num(0);
        viewpointMsg.setClose_status(CloseStatus.NORMAL.getVal());
        viewpointMsg.setViewpoint_tag(viewpoint_tag_id);
        viewpointMsg.setViewpoint_topic_id(viewpoint_topic_id);
        viewpointMsgService.save(viewpointMsg);
        if (en_combi_code!=null && !en_combi_code.isEmpty()) {
            String[] combiCodeList = en_combi_code.split(",");
            for (String code : combiCodeList) {
                CombinationEntity combinationEntity = combineService.getCombinationByCode(code);
                if (combinationEntity == null ) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return VOUtil.genErrorResult(HSBCPSErrorCodes.CombinationErrorInfo.COMBINATION_NOT_EXIST,
                            ViewpointSaveResult.class);
                }
                ViewpointCombination viewpointCombination = new ViewpointCombination();
                viewpointCombination.setCombination_id(combinationEntity.getCombination_id());
                viewpointCombination.setViewpointmsg_id(viewpointMsg.getViewpoint_id());
                viewpointCombination.setCombination_code(code);
                viewpointCombination.setCompany_id(company_id);
                viewpointCombinationService.save(viewpointCombination);
            }
        }
        //推送微信模板消息
        if (viewpointSaveForm.getCompany_id().equals("20020")) {
            XinanPushForm xpf = new XinanPushForm();
            xpf.setTag(String.valueOf(user_id));
            xpf.setUserRemindType(UserRemindType.LEADER);
            xpf.setXinanPushType(XinanPushType.VIEWPOINT);
            xpf.setCompany_id(viewpointSaveForm.getCompany_id());
            xpf.setViewpoint_theme(viewpointMsg.getViewpoint_theme());
            pushService.xinanTemplatePush(xpf);
        }
        ViewpointSaveResult saveResult = new ViewpointSaveResult();
        return VOUtil.setSuccessResult(saveResult);
    }

    /**
     * 观点展示(投顾)
     */
    public ViewpointList funcViewpointList(ViewpointQueryForm viewpointQueryForm) {
        viewpointQueryForm.setUser_id(viewpointQueryForm.getUser().getUser_id());
        List<ViewpointMsg> viewpointMsgs = viewpointMsgService.queryByKeyWord(viewpointQueryForm);
        List<ViewpointListData> viewpointListDataList = new ArrayList<>();

        for(ViewpointMsg viewpointVo: viewpointMsgs) {
            ViewpointListData viewpointListView = new ViewpointListData();
            viewpointListView.setViewpoint_id(viewpointVo.getViewpoint_id());
            viewpointListView.setViewpoint_theme(viewpointVo.getViewpoint_theme());
            viewpointListView.setViewpoint_content(viewpointVo.getViewpoint_content());
            viewpointListView.setFile_path(viewpointVo.getViewpoint_image_keys());
            viewpointListView.setUpvote_num(viewpointVo.getUpvote_num());
            viewpointListView.setShare_num(viewpointVo.getShare_num());
            viewpointListView.setUpdate_time(viewpointVo.getUpdate_time());
            viewpointListView.setClose_status(viewpointVo.getClose_status());
            viewpointListView.setVoice_path(viewpointVo.getVoice_path());
            viewpointListView.setViewpoint_type(viewpointVo.getViewpoint_type());
            viewpointListView.setViewpoint_topic_id(viewpointVo.getViewpoint_topic_id());
            viewpointListView.setViewpoint_tag_id(viewpointVo.getViewpoint_tag());
            //从后台获取组合代码
            String en_combi_code = "";
            ViewpointCombination viewpointCombination1 = new ViewpointCombination();
            viewpointCombination1.setViewpointmsg_id(viewpointVo.getViewpoint_id());
            List<ViewpointCombination> viewpointCombinations =
                    viewpointCombinationService.queryByViewpoint(viewpointCombination1);
            if (viewpointCombinations!=null && viewpointCombinations.size() > 0 ) {
                for (ViewpointCombination viewpointCombination : viewpointCombinations) {
                    en_combi_code += viewpointCombination.getCombination_code() + ",";
                }
                en_combi_code = en_combi_code.substring(0,en_combi_code.length()-1);
            }
            viewpointListView.setEn_combi_code(en_combi_code);
            viewpointListDataList.add(viewpointListView);
        }
        setLeaderViewpointTopicAndTagInfo(viewpointListDataList);
        ViewpointList viewpointList = new ViewpointList();
        viewpointList.setTotal_item_num(viewpointMsgService.countByKeyword(viewpointQueryForm));
        viewpointList.setData_list(viewpointListDataList);

        return VOUtil.setSuccessResult(viewpointList);
    }

    /**
     * 观点展示(粉丝)
     */
    public ViewpointRecomList funcViewpointRecomList(ViewpointRecomQueryForm viewpointRecomQueryForm) {
        //获取观点列表
        ViewpointMsg viewpointMsg = new ViewpointMsg();
        viewpointMsg.setCompany_id(viewpointRecomQueryForm.getCompany_id());
        viewpointMsg.setClose_status(CloseStatus.NORMAL.getVal());
        Pagination page = new Pagination();
        page.setPage_no(viewpointRecomQueryForm.getPage_no());
        page.setPage_size(viewpointRecomQueryForm.getRequest_num());
        page.setPosition_str(viewpointRecomQueryForm.getPosition_str());
        //添加观点话题筛选
        if (null != viewpointRecomQueryForm.getViewpoint_topic_id()&& !"".equals(viewpointRecomQueryForm.getViewpoint_topic_id())){
            viewpointMsg.setViewpoint_topic_id(Long.valueOf(viewpointRecomQueryForm.getViewpoint_topic_id()));
        }
        if (null != viewpointRecomQueryForm.getViewpoint_tag_id()&& !"".equals(viewpointRecomQueryForm.getViewpoint_tag_id())){
            viewpointMsg.setViewpoint_tag(Long.valueOf(viewpointRecomQueryForm.getViewpoint_tag_id()));

        }
        if (null != viewpointRecomQueryForm.getViewpoint_type()&& !"".equals(viewpointRecomQueryForm.getViewpoint_type())){
            viewpointMsg.setViewpoint_type(viewpointRecomQueryForm.getViewpoint_type());

        }
        List<ViewpointMsg> viewpointMsgs = viewpointMsgService.queryWithPagination(viewpointMsg,page);

        //观点datalist组装
        List<ViewpointRecomListData> viewpointRecomListDatas = toFansViewpointList(viewpointMsgs);
        //投顾信息插入
        setLeaderInfo(viewpointRecomListDatas,viewpointRecomQueryForm.getUser_token());
        //添加话题信息
        setViewpointTopicAndTagInfo(viewpointRecomListDatas);
        //插入关注信息
        if (viewpointRecomQueryForm.getUser()!=null){
            setUpvoteInfo(viewpointRecomListDatas,viewpointRecomQueryForm.getUser().getUser_id());
        }else {
            setUpvoteInfo(viewpointRecomListDatas,null);
        }
        //result组装
        ViewpointRecomList viewpointRecomList = new ViewpointRecomList();
        viewpointRecomList.setData_list(viewpointRecomListDatas);
        viewpointRecomList.setPage_no(page.getPage_no());
        viewpointRecomList.setTotal_item_num(page.getTotal_item_num());
        viewpointRecomList.setPage_size(page.getPage_size());
        return VOUtil.setSuccessResult(viewpointRecomList);

    }

    @Transactional
    public ViewpointUpvoteResult funcViewpointUpvote(ViewpointUpvoteForm viewpointUpvoteForm){
        String viewpoint_id = viewpointUpvoteForm.getViewpoint_id();
        Long user_id = viewpointUpvoteForm.getUser().getUser_id();
        ViewpointMsg viewpointMsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointMsg == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    ViewpointUpvoteResult.class);
        }
        //点赞与取消
        ViewpointUpvote viewpointUpvote = new ViewpointUpvote();
        viewpointUpvote.setCompany_id(viewpointMsg.getCompany_id());
        viewpointUpvote.setUser_id(viewpointUpvoteForm.getUser().getUser_id());
        viewpointUpvote.setViewpointmsg_id(viewpoint_id);
        //根据upvote_flag进行判断进行点赞还是取消
        if (viewpointUpvoteForm.getUpvote_flag().equals(UpvoteFlag.UPVOTE.getVal())) {
            //是否点过赞
            if (UpvoteCacheUtil.isUpvoteViewpoint(Long.valueOf(viewpoint_id),user_id)){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_ALREADY_UPVOTE,
                        ViewpointUpvoteResult.class);
            }
            //数据库同步
            UpvoteRecord upvoteRecord = new UpvoteRecord();
            upvoteRecord.setCompany_id(viewpointUpvoteForm.getCompany_id());
            upvoteRecord.setUser_id(user_id);
            upvoteRecord.setUpvote_target_id(Long.valueOf(viewpoint_id));
            upvoteRecord.setUpvote_type(String.valueOf(UpvoteType.VIEWPOINT_UPVOTE.getVal()));
            upvoteRecord.setCreate_date(System.currentTimeMillis());
            upvoteRecordService.save(upvoteRecord);
            ViewpointMsg viewpointMsg1 = new ViewpointMsg();
            viewpointMsg1.setViewpoint_id(viewpointMsg.getViewpoint_id());
            viewpointMsg1.setCompany_id(viewpointMsg.getCompany_id());
            viewpointMsg1.setUpvote_num(viewpointMsg.getUpvote_num() + 1);
            viewpointMsgService.update(viewpointMsg1);
            //插入缓存
            UpvoteCacheUtil.addViewpointUpvoteRecord(Long.valueOf(viewpoint_id),user_id);
        }else if (viewpointUpvoteForm.getUpvote_flag().equals(UpvoteFlag.CANCEL_UPVOTE.getVal())){
            if (!UpvoteCacheUtil.isUpvoteViewpoint(Long.valueOf(viewpoint_id),user_id)){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_HAVE_NOT_UPVOTE,
                        ViewpointUpvoteResult.class);
            }
            //数据库同步
            UpvoteRecord upvoteRecord = new UpvoteRecord();
            upvoteRecord.setCompany_id(viewpointUpvoteForm.getCompany_id());
            upvoteRecord.setUser_id(user_id);
            upvoteRecord.setUpvote_target_id(Long.valueOf(viewpoint_id));
            upvoteRecord.setUpvote_type(String.valueOf(UpvoteType.VIEWPOINT_UPVOTE.getVal()));
            int delete_num = upvoteRecordService.cancelUpvote(upvoteRecord);
            if (delete_num > 0) {
                ViewpointMsg viewpointMsg1 = new ViewpointMsg();
                viewpointMsg1.setViewpoint_id(viewpointMsg.getViewpoint_id());
                viewpointMsg1.setUpvote_num(viewpointMsg.getUpvote_num() - 1);
                viewpointMsg1.setCompany_id(viewpointMsg.getCompany_id());
                viewpointMsgService.update(viewpointMsg1);
            }
            UpvoteCacheUtil.delViewpointUpvoteRecord(Long.valueOf(viewpoint_id),user_id);
        }else {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_UPVOTE_FLAG_ERROR,
                    ViewpointUpvoteResult.class);
        }
        ViewpointUpvoteResult viewpointUpvoteResult = new ViewpointUpvoteResult();
        return VOUtil.setSuccessResult(viewpointUpvoteResult);
    }



    @Override
    @Transactional
    public ViewpointSaveResult funcViewpointUpdate(ViewpointUpdateForm viewpointUpdateForm) {

        ViewpointSaveResult viewpointSaveResult = new ViewpointSaveResult();

        String viewpoint_id = viewpointUpdateForm.getViewpoint_id();
        String viewpoint_theme = viewpointUpdateForm.getViewpoint_theme();
        String viewpoint_content = viewpointUpdateForm.getViewpoint_content();
        String viewpoint_filePath = viewpointUpdateForm.getFile_path();
        String viewpoint_en_combi_code = viewpointUpdateForm.getEn_combi_code();
        Long viewpoint_topic_id = viewpointUpdateForm.getViewpoint_topic_id();
        Long viewpoint_tag_id = viewpointUpdateForm.getViewpoint_tag_id();
        String voice_path = viewpointUpdateForm.getVoice_path();
        String viewpoint_type  =  "".equals(viewpointUpdateForm.getViewpoint_type())?"1":viewpointUpdateForm.getViewpoint_type();
        //组装
        ViewpointMsg viewpointMsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointMsg == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    ViewpointSaveResult.class);
        }
        if (viewpoint_type.equals("1") &&
                (StringUtil.isBlank(viewpointUpdateForm.getViewpoint_theme())||
                StringUtil.isBlank(viewpointUpdateForm.getViewpoint_content()))){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PARAM_ERROR,"观点标题和观点内容不能为空",ViewpointSaveResult.class);
        }
        //不能修改别人的观点
        if(viewpointMsg.getUser_id().longValue() != viewpointUpdateForm.getUser().getUser_id().longValue()){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_UPDATE_NOT_AUTH, ViewpointSaveResult.class);
        }

        viewpointMsg.setViewpoint_content(viewpoint_content);
        viewpointMsg.setViewpoint_theme(viewpoint_theme);
        viewpointMsg.setViewpoint_image_keys(viewpoint_filePath);
        viewpointMsg.setUpdate_time(System.currentTimeMillis());
        viewpointMsg.setVoice_path(voice_path);
        viewpointMsg.setViewpoint_topic_id(viewpoint_topic_id);
        viewpointMsg.setViewpoint_tag(viewpoint_tag_id);
        viewpointMsg.setViewpoint_type(viewpoint_type);
        ViewpointCombination viewpointCombination1 = new ViewpointCombination();
        viewpointCombination1.setViewpointmsg_id(viewpointMsg.getViewpoint_id());
        viewpointCombination1.setCompany_id(viewpointMsg.getCompany_id());
        viewpointCombinationService.deleteByViewpoint(viewpointCombination1);
        //观点组合修改
        if (viewpoint_en_combi_code!=null && !viewpoint_en_combi_code.isEmpty()) {
            String[] combiCodeList = viewpoint_en_combi_code.split(",");
            for (String code : combiCodeList) {
                CombinationEntity combinationEntity = combineService.getCombinationByCode(code);
                if (combinationEntity == null ) {
                    return VOUtil.genErrorResult(HSBCPSErrorCodes.CombinationErrorInfo.COMBINATION_NOT_EXIST,
                            ViewpointSaveResult.class);
                }
                ViewpointCombination viewpointCombination = new ViewpointCombination();
                viewpointCombination.setCompany_id(viewpointMsg.getCompany_id());
                viewpointCombination.setCombination_id(combinationEntity.getCombination_id());
                viewpointCombination.setViewpointmsg_id(viewpointMsg.getViewpoint_id());
                viewpointCombination.setCombination_code(code);
                viewpointCombinationService.save(viewpointCombination);
            }
        }
        viewpointMsgService.update(viewpointMsg);
        return VOUtil.setSuccessResult(viewpointSaveResult);
    }

    @Override
    public ViewpointShareResult funcViewpointShare(ViewpointShareForm viewpointShareForm){
        String viewpoint_id = viewpointShareForm.getViewpoint_id();
        ViewpointMsg viewpointMsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointMsg == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    ViewpointShareResult.class);
        }
        viewpointMsg.setShare_num(viewpointMsg.getShare_num() + 1);
        viewpointMsgService.update(viewpointMsg);

        ViewpointShareResult viewpointShareResult = new ViewpointShareResult();
        return VOUtil.setSuccessResult(viewpointShareResult);
    }

    @Override
    public ViewpointRecomList funcQueryAdviserViewpoint(AdviserViewpointQueryForm adviserViewpointQueryForm) {
        ViewpointMsg viewpointMsg = new ViewpointMsg();
        Long adviser_id = Long.valueOf(adviserViewpointQueryForm.getAdviser_id());
        String company_id = adviserViewpointQueryForm.getCompany_id();
        viewpointMsg.setClose_status(CloseStatus.NORMAL.getVal());
        viewpointMsg.setUser_id(adviser_id);
        viewpointMsg.setCompany_id(company_id);
        //分页
        Pagination page = new Pagination();
        page.setPage_no(adviserViewpointQueryForm.getPage_no());
        page.setPage_size(adviserViewpointQueryForm.getRequest_num());

        List<ViewpointMsg> viewpointMsgs = viewpointMsgService.queryWithPagination(viewpointMsg,page);
        //返回组装

        List<ViewpointRecomListData> viewpointRecomListDatas = toFansViewpointList(viewpointMsgs);
        //加入投顾信息
        setLeaderInfo(viewpointRecomListDatas,adviserViewpointQueryForm.getUser_token());
        if (adviserViewpointQueryForm.getUser()!=null){
            setUpvoteInfo(viewpointRecomListDatas,adviserViewpointQueryForm.getUser().getUser_id());
        }else {
            setUpvoteInfo(viewpointRecomListDatas,null);
        }
        setViewpointTopicAndTagInfo(viewpointRecomListDatas);
        //result加入datalist
        ViewpointRecomList viewpointRecomList = new ViewpointRecomList();
        viewpointRecomList.setTotal_item_num(page.getTotal_item_num());
        viewpointRecomList.setData_list(viewpointRecomListDatas);
        return VOUtil.setSuccessResult(viewpointRecomList);
    }

    @Override
    public ViewpointRecomList funcQueryRecommendViewpoint(ViewpointRecomQueryForm viewpointRecomQueryForm) {
        ViewpointMsg viewpointMsg = new ViewpointMsg();
        String company_id = viewpointRecomQueryForm.getCompany_id();
        viewpointMsg.setClose_status(CloseStatus.NORMAL.getVal());
        viewpointMsg.setCompany_id(company_id);

        viewpointRecomQueryForm.setClose_status(CloseStatus.NORMAL.getVal());
        RecommendRule recommendRule = new RecommendRule();
        recommendRule.setRecommend_biz_type(RecommendBizType.VIEWPOINT.getVal());
        recommendRule.setCompany_id(viewpointRecomQueryForm.getCompany_id());
        recommendRule = recommendRuleService.getByEntity(recommendRule);
        List<ViewpointMsg> viewpointMsgs = new ArrayList<>();

        if (recommendRule == null){
            Pagination page = new Pagination();
            page.setPage_no(1);
            page.setPage_size(3);
            viewpointMsg.setPage(page);
            viewpointMsgs =viewpointMsgService.query(viewpointMsg);
        }else if(recommendRule.getRecommend_order_type().equals(ViewpointRecomOrderType.CUSTOM.getVal())){
            viewpointMsgs = viewpointRecomService.queryViewpointRecomList(viewpointRecomQueryForm);
        }else if(recommendRule.getRecommend_order_type().equals(ViewpointRecomOrderType.UP_VOTE_TIMES.getVal())) {
            Pagination page = new Pagination();
            page.setPage_no(1);
            page.setPage_size(3);
            viewpointMsg.setPage(page);
            SortMarker sortMarker = new SortMarker();
            sortMarker.setField("upvote_num");
            sortMarker.setAsc(false);
            viewpointMsg.addSortMarker(sortMarker);
            viewpointMsgs =viewpointMsgService.query(viewpointMsg);
        }

            //datalist组装
        List<ViewpointRecomListData> viewpointRecomListDatas = toFansViewpointList(viewpointMsgs);

        //加入投顾信息
        setLeaderInfo(viewpointRecomListDatas,viewpointRecomQueryForm.getUser_token());

        if (viewpointRecomQueryForm.getUser()!=null){
            setUpvoteInfo(viewpointRecomListDatas,viewpointRecomQueryForm.getUser().getUser_id());
        }else {
            setUpvoteInfo(viewpointRecomListDatas,null);
        }
        //result加入datalist
        ViewpointRecomList viewpointRecomList = new ViewpointRecomList();
        viewpointRecomList.setData_list(viewpointRecomListDatas);
        return VOUtil.setSuccessResult(viewpointRecomList);
    }

    //粉丝端返回数据组装
    private List<ViewpointRecomListData> toFansViewpointList(List<ViewpointMsg> viewpointMsgs) {
        List<ViewpointRecomListData> viewpointRecomListDatas =
                new ArrayList<>(viewpointMsgs.size());
        for(ViewpointMsg viewpointMsgVo : viewpointMsgs) {
            ViewpointRecomListData viewpointListData  = new ViewpointRecomListData();
            viewpointListData.setViewpoint_id(viewpointMsgVo.getViewpoint_id());
            viewpointListData.setViewpoint_theme(viewpointMsgVo.getViewpoint_theme());
            viewpointListData.setViewpoint_content(viewpointMsgVo.getViewpoint_content());
            viewpointListData.setFile_path(viewpointMsgVo.getViewpoint_image_keys());
            viewpointListData.setUpvote_num(viewpointMsgVo.getUpvote_num());
            viewpointListData.setUpdate_time(viewpointMsgVo.getUpdate_time());
            viewpointListData.setShare_num(viewpointMsgVo.getShare_num());
            viewpointListData.setLeader_user_id(viewpointMsgVo.getUser_id());
            viewpointListData.setSend_time(viewpointMsgVo.getSend_time());
            viewpointListData.setViewpoint_tag_id(viewpointMsgVo.getViewpoint_tag()==null?null:Long.valueOf(viewpointMsgVo.getViewpoint_tag()));
            viewpointListData.setViewpoint_topic_id(viewpointMsgVo.getViewpoint_topic_id());
            viewpointListData.setVoice_path(viewpointMsgVo.getVoice_path());
            viewpointListData.setViewpoint_type(viewpointMsgVo.getViewpoint_type());
            //从获取组合代码
            String en_combi_code = getCombinationCode(viewpointMsgVo.getViewpoint_id());

            viewpointListData.setEn_combi_code(en_combi_code);
            viewpointRecomListDatas.add(viewpointListData);
        }
        return viewpointRecomListDatas;
    }


    @Override
    public ViewpointSaveResult funcCloseViewpoint(ViewPointIdForm form){
        String viewpoint_id = form.getViewpoint_id();
        ViewpointMsg viewpointMsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointMsg == null ) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    ViewpointSaveResult.class);

        }
        //不能关闭不是自己的观点
        if(viewpointMsg.getUser_id().longValue() != form.getUser().getUser_id().longValue()){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_CLOSE_NOT_AUTH, ViewpointSaveResult.class);
        }
        viewpointMsg.setClose_status(CloseStatus.CLOSE.getVal());
        viewpointMsgService.update(viewpointMsg);
        ViewpointSaveResult viewpointSaveResult = new ViewpointSaveResult();
        return VOUtil.setSuccessResult(viewpointSaveResult);
    }

    @Override
    public ViewpointSaveResult funcCancelCloseViewpoint(ViewPointIdForm form) {
        String viewpoint_id = form.getViewpoint_id();
        ViewpointMsg viewpointMsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointMsg == null ) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    ViewpointSaveResult.class);

        }
        //不能取消不是自己的观点
        if(!viewpointMsg.getUser_id().equals(form.getUser().getUser_id())){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_CANCEL_NOT_AUTH, ViewpointSaveResult.class);
        }
        viewpointMsg.setClose_status(CloseStatus.NORMAL.getVal());
        viewpointMsgService.update(viewpointMsg);
        ViewpointSaveResult viewpointSaveResult = new ViewpointSaveResult();
        return VOUtil.setSuccessResult(viewpointSaveResult);
    }


    public ViewpointDetailResult funcViewpointDetail(ViewPointIdForm form) {
        String viewpoint_id = form.getViewpoint_id();
        ViewpointDetailResult viewpointDetailResult = new ViewpointDetailResult();
        ViewpointMsg viewpointmsg = viewpointMsgService.get(viewpoint_id);
        if (viewpointmsg == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,ViewpointDetailResult.class);
        }
        viewpointDetailResult.setViewpoint_content(viewpointmsg.getViewpoint_content());
        viewpointDetailResult.setViewpoint_theme(viewpointmsg.getViewpoint_theme());
        viewpointDetailResult.setViewpoint_id(viewpointmsg.getViewpoint_id());
        viewpointDetailResult.setFile_path(viewpointmsg.getViewpoint_image_keys());
        viewpointDetailResult.setEn_combi_code(getCombinationCode(viewpointmsg.getViewpoint_id()));
        viewpointDetailResult.setUpdate_time(viewpointmsg.getUpdate_time());
        viewpointDetailResult.setShare_num(viewpointmsg.getShare_num());
        viewpointDetailResult.setUpvote_num(viewpointmsg.getUpvote_num());
        viewpointDetailResult.setSend_time(viewpointmsg.getSend_time());
        viewpointDetailResult.setViewpoint_tag_id(viewpointmsg.getViewpoint_tag());
        viewpointDetailResult.setViewpoint_topic_id(viewpointmsg.getViewpoint_topic_id());
        viewpointDetailResult.setVoice_path(viewpointmsg.getVoice_path());
        viewpointDetailResult.setLeader_user_id(viewpointmsg.getUser_id());
        //添加观点标签信息
        if(viewpointmsg.getViewpoint_tag()!=null && !"".equals(viewpointmsg.getViewpoint_tag())){
            ViewpointTag viewpointTag = new ViewpointTag();
            viewpointTag.setViewpoint_tag_id(Long.valueOf(viewpointmsg.getViewpoint_tag()));
            viewpointTag.setCompany_id(form.getCompany_id());
            viewpointTag = viewpointTagService.getByEntity(viewpointTag);
            if (viewpointTag!=null)
                viewpointDetailResult.setViewpoint_tag_name(viewpointTag.getTag_name());
        }

        //添加话题标签信息Topic
        if(viewpointmsg.getViewpoint_topic_id()!=null && !"".equals(viewpointmsg.getViewpoint_topic_id())){
            ViewpointTopic viewpointTopic = new ViewpointTopic();
            viewpointTopic.setViewpoint_topic_id(Long.valueOf(viewpointmsg.getViewpoint_topic_id()));
            viewpointTopic.setCompany_id(form.getCompany_id());
            viewpointTopic = viewpointTopicService.getByEntity(viewpointTopic);
            if (viewpointTopic!=null)
                viewpointDetailResult.setViewpoint_topic_name(viewpointTopic.getTopic_name());
        }

        //添加leader信息

        UsersQueryVo usersQueryVo = userCenterService.func_users_query(String.valueOf(viewpointmsg.getUser_id()));
        if (usersQueryVo.getData_list()!=null && !usersQueryVo.getData_list().isEmpty()) {
            UserEntityVO userEntity = usersQueryVo.getData_list().get(0);
            userEntity.setAlias_name(com.cloudbroker.bcs.platform.ltb.middle.util.StringUtil.replaceMobile(userEntity.getAlias_name()));//修改单号M201701170424
            viewpointDetailResult.setLeader_avatar(userEntity.getAvatar());
            viewpointDetailResult.setLeader_name(userEntity.getAlias_name());
        }
        InterestDetailForm interestDetailForm = new InterestDetailForm();
        interestDetailForm.setUser_token(form.getUser_token());
        interestDetailForm.setUser_id(String.valueOf(viewpointmsg.getUser_id()));
        interestDetailForm.setSendercomp_id(form.getSendercomp_id());
        InterestDetailVo interestDetailVo = lightinvestingService.func_interest_detail_query(interestDetailForm);
        viewpointDetailResult.setAdviser_type(interestDetailVo.getAdviser_type());
        if (form.getUser() == null || form.getUser().getUser_id() == null){
            viewpointDetailResult.setUpvote_flag(UpvoteStatus.NOT_UPVOTE.getVal());
        } else {
            boolean isUpvote = UpvoteCacheUtil.isUpvoteViewpoint(viewpointmsg.getViewpoint_id(),
                    form.getUser().getUser_id());
            viewpointDetailResult.setUpvote_flag(
                    isUpvote ? UpvoteStatus.ALREADY_UPVOTE.getVal() : UpvoteStatus.NOT_UPVOTE.getVal());
        }
        return VOUtil.setSuccessResult(viewpointDetailResult);

    }


    //加入投顾信息
    private void setLeaderInfo(List<ViewpointRecomListData> viewpointRecomListDatas,String user_token) {
        UserCache UserCache = UserTokenUtil.queryUserToken(user_token);
        String sendercomp_id = UserCache.getBrokerVO().getSendercomp_id();
        //投顾信息的缓存，减少数据库读取
        Map<String, String[]> leaderInfoMap = new HashMap<>();
        //获取用户中心的昵称头像
        List<Long> user_id_list = new ArrayList<>();
        for (ViewpointRecomListData viewpointRecomListData : viewpointRecomListDatas){
            user_id_list.add(viewpointRecomListData.getLeader_user_id());
        }
        String user_ids = StringUtils.join(user_id_list,",");
        Map<Long,UserEntityVO> userMap = new HashMap();
        UsersQueryVo usersQueryVo = userCenterService.func_users_query(user_ids);
        if (VOUtil.isSuccess(usersQueryVo)){
            for (UserEntityVO user : usersQueryVo.getData_list()){
                user.setAlias_name(com.cloudbroker.bcs.platform.ltb.middle.util.StringUtil.replaceMobile(user.getAlias_name()));//修改单号M201701170424
                userMap.put(user.getUser_id(),user);
            }
        }
        for (ViewpointRecomListData viewpointRecomListData : viewpointRecomListDatas){
            //添加投顾信息（优先从缓存map中获取）
            String leader_user_id = String.valueOf(viewpointRecomListData.getLeader_user_id());
            if (leaderInfoMap.containsKey(leader_user_id)) {
                viewpointRecomListData.setLeader_name(leaderInfoMap.get(leader_user_id)[0]);
                viewpointRecomListData.setLeader_avatar(leaderInfoMap.get(leader_user_id)[1]);
                viewpointRecomListData.setAdviser_type(leaderInfoMap.get(leader_user_id)[2]);
            }else {
                //缓存中不存在则获取
                UserEntityVO userEntity = userMap.get(viewpointRecomListData.getLeader_user_id());
                String leader_name = "";
                String leader_avatar = "";
                if (userEntity !=null) {
                    leader_name = userEntity.getAlias_name();
                    leader_avatar = userEntity.getAvatar() == null ? "" : userEntity.getAvatar();
                    viewpointRecomListData.setLeader_name(leader_name);
                    viewpointRecomListData.setLeader_avatar(leader_avatar);
                }
                //获取投顾类型
                InterestDetailForm interestDetailForm = new InterestDetailForm();
                interestDetailForm.setUser_token(user_token);
                interestDetailForm.setUser_id(leader_user_id);
                interestDetailForm.setSendercomp_id(sendercomp_id);
                InterestDetailVo interestDetailVo = lightinvestingService.func_interest_detail_query(interestDetailForm);
                viewpointRecomListData.setAdviser_type(interestDetailVo.getAdviser_type());
                //添加到投顾缓存
                String[] leaderInfo = {leader_name, leader_avatar, interestDetailVo.getAdviser_type()};
                leaderInfoMap.put(leader_user_id, leaderInfo);
            }
        }
    }

    //
    private String getCombinationCode(Long viewpoint_id) {
        String en_combi_code = "";
        ViewpointCombination viewpointCombination1 = new ViewpointCombination();
        viewpointCombination1.setViewpointmsg_id(viewpoint_id);
        List<ViewpointCombination> viewpointCombinations =
                viewpointCombinationService.queryByViewpoint(viewpointCombination1);
        if (viewpointCombinations!=null && viewpointCombinations.size() > 0 ) {
            for (ViewpointCombination viewpointCombination : viewpointCombinations) {
                en_combi_code += viewpointCombination.getCombination_code() + ",";
            }
            en_combi_code = en_combi_code.substring(0,en_combi_code.length()-1);
        }
        return en_combi_code;
    }

    private void setUpvoteInfo(List<ViewpointRecomListData> viewpointRecomListDatas,Long user_id){
        //游客默认全未点赞
        if (user_id!=null) {
            for (ViewpointRecomListData viewpointRecomListData : viewpointRecomListDatas) {
                boolean isUpvote = UpvoteCacheUtil.isUpvoteViewpoint(viewpointRecomListData.getViewpoint_id(),
                        user_id);
                viewpointRecomListData.setUpvote_flag(
                        isUpvote ? UpvoteStatus.ALREADY_UPVOTE.getVal() : UpvoteStatus.NOT_UPVOTE.getVal());
            }
        }else {
            for (ViewpointRecomListData viewpointRecomListData : viewpointRecomListDatas) {
                viewpointRecomListData.setUpvote_flag(UpvoteStatus.NOT_UPVOTE.getVal());
            }
        }
    }

    public ViewpointTagListResult funcQueryViewpointTag(ViewpointTagQueryForm form){
        ViewpointTagListResult viewpointTagListResult = new ViewpointTagListResult();
        List<ViewpointTagData> viewpointTagDataList = new ArrayList<>();
        ViewpointTag viewpointTag = new ViewpointTag();
        viewpointTag.setCompany_id(form.getCompany_id());
        viewpointTag.setStatus(CloseStatus.NORMAL.getVal());
        List<ViewpointTag> viewpointTagList = viewpointTagService.query(viewpointTag);
        if (viewpointTagList!=null && viewpointTagList.size()>0){
            for (ViewpointTag temp:viewpointTagList) {
                ViewpointTagData viewpointTagData = new ViewpointTagData();
                viewpointTagData.setViewpoint_tag_id(temp.getViewpoint_tag_id());
                viewpointTagData.setTag_name(temp.getTag_name());
                viewpointTagData.setOrder_num(temp.getOrder_num());
                viewpointTagDataList.add(viewpointTagData);
            }
        }
        viewpointTagListResult.setData_list(viewpointTagDataList);
        return VOUtil.setSuccessResult(viewpointTagListResult);
    }

    public ViewpointTopicListResult funcQueryViewpointTopic(ViewpointTopicQueryForm form){
        ViewpointTopicListResult viewpointTopicListResult = new ViewpointTopicListResult();
        List<ViewpointTopicData> viewpointTopicDataList = new ArrayList<>();
        ViewpointTopic viewpointTopic = new ViewpointTopic();
        viewpointTopic.setStatus(CloseStatus.NORMAL.getVal());
        viewpointTopic.setCompany_id(form.getCompany_id());
        Pagination page = new Pagination();
        page.setPage_size(form.getRequest_num());
        page.setPage_no(form.getPage_no());
        viewpointTopic.setPage(page);
        List<ViewpointTopic> viewpointTopicList = viewpointTopicService.query(viewpointTopic);
        if (viewpointTopicList!=null && viewpointTopicList.size()>0){
            for (ViewpointTopic temp : viewpointTopicList) {
                ViewpointTopicData viewpointTopicData = new ViewpointTopicData();
                viewpointTopicData.setOrder_num(temp.getOrder_num());
                viewpointTopicData.setTopic_name(temp.getTopic_name());
                viewpointTopicData.setViewpoint_topic_id(temp.getViewpoint_topic_id());
                viewpointTopicData.setTopic_image(temp.getTopic_image());
                viewpointTopicDataList.add(viewpointTopicData);
            }
        }
        viewpointTopicListResult.setData_list(viewpointTopicDataList);
        return VOUtil.setSuccessResult(viewpointTopicListResult);
    }

    public ViewpointTopicSaveResult funcSaveViewpointTopic(ViewpointTopicSaveForm form){
        ViewpointTopic viewpointTopic = new ViewpointTopic();
        viewpointTopic.setCompany_id(form.getCompany_id());
        viewpointTopic.setTopic_name(form.getViewpoint_topic_name());
        viewpointTopic.setCreate_time(System.currentTimeMillis());
        viewpointTopic.setOrder_num(0);
        viewpointTopic.setStatus(CloseStatus.NORMAL.getVal());
        viewpointTopicService.save(viewpointTopic);
        ViewpointTopicSaveResult viewpointTopicSaveResult =new ViewpointTopicSaveResult();
        return VOUtil.setSuccessResult(viewpointTopicSaveResult);
    }

    public ViewpointTopicSaveResult funcUpdateViewpointTopic(ViewpointTopicUpdateForm form){
        ViewpointTopic viewpointTopic = new ViewpointTopic();
        viewpointTopic.setViewpoint_topic_id(Long.valueOf(form.getViewpoint_topic_id()));
        viewpointTopic = viewpointTopicService.getByEntity(viewpointTopic);
        viewpointTopic.setCompany_id(form.getCompany_id());
        viewpointTopic.setTopic_name(form.getViewpoint_topic_name());
        viewpointTopicService.update(viewpointTopic);
        ViewpointTopicSaveResult viewpointTopicSaveResult =new ViewpointTopicSaveResult();
        return VOUtil.setSuccessResult(viewpointTopicSaveResult);
    }

    public ViewpointTopicSaveResult funcCloseViewpointTopic(ViewpointTopicCloseForm form){
        ViewpointTopic viewpointTopic = new ViewpointTopic();
        viewpointTopic.setViewpoint_topic_id(Long.valueOf(form.getViewpoint_topic_id()));
        viewpointTopic.setStatus(CloseStatus.CLOSE.getVal());
        viewpointTopicService.update(viewpointTopic);
        ViewpointTopicSaveResult viewpointTopicSaveResult =new ViewpointTopicSaveResult();
        return VOUtil.setSuccessResult(viewpointTopicSaveResult);
    }

    public ViewpointTagSaveResult funcCloseViewpointTag(ViewpointTagCloseForm form){
        ViewpointTag viewpointTag = new ViewpointTag();
        viewpointTag.setViewpoint_tag_id(Long.valueOf(form.getViewpoint_tag_id()));
        viewpointTag.setStatus(CloseStatus.CLOSE.getVal());
        viewpointTagService.update(viewpointTag);
        ViewpointTagSaveResult viewpointTagSaveResult =new ViewpointTagSaveResult();
        return VOUtil.setSuccessResult(viewpointTagSaveResult);
    }

    public ViewpointTagSaveResult funcUpdateViewpointTag(ViewpointTagUpdateForm form){
        ViewpointTag viewpointTag = new ViewpointTag();
        viewpointTag.setCompany_id(form.getCompany_id());
        viewpointTag.setViewpoint_tag_id(Long.valueOf(form.getTag_id()));
        viewpointTag.setTag_name(form.getTag_name());
        viewpointTag.setTag_summary(form.getTag_summary());
        viewpointTagService.update(viewpointTag);
        ViewpointTagSaveResult viewpointTagSaveResult =new ViewpointTagSaveResult();
        return VOUtil.setSuccessResult(viewpointTagSaveResult);
    }

    public ViewpointTopicDetailResult funcViewpointTopicDetail(ViewpointTopicDetailForm form){
        ViewpointTopic viewpointTopicVo = new ViewpointTopic();
        viewpointTopicVo.setViewpoint_topic_id(Long.valueOf(form.getViewpoint_topic_id()));
        viewpointTopicVo.setCompany_id(form.getCompany_id());
        ViewpointTopic viewpointTopic = viewpointTopicService.getByEntity(viewpointTopicVo);
        ViewpointTopicDetailResult result = new ViewpointTopicDetailResult();
        result.setCompany_id(viewpointTopic.getCompany_id());
        result.setViewpoint_topic_id(viewpointTopic.getViewpoint_topic_id());
        result.setCreate_time(viewpointTopic.getCreate_time());
        result.setSummary(viewpointTopic.getSummary());
        result.setTopic_name(viewpointTopic.getTopic_name());
        result.setTopic_image(viewpointTopic.getTopic_image());
        return VOUtil.setSuccessResult(result);
    }

    public ViewpointTagSaveResult funcSaveViewpointTag(ViewpointTagForm form){
        ViewpointTag viewpointTag = new ViewpointTag();
        viewpointTag.setCompany_id(form.getCompany_id());
        viewpointTag.setTag_name(form.getTag_name());
        viewpointTag.setTag_summary(form.getTag_summary());
        viewpointTag.setCreate_time(System.currentTimeMillis());
        viewpointTag.setStatus(CloseStatus.NORMAL.getVal());
        viewpointTag.setOrder_num(0);
        viewpointTagService.save(viewpointTag);
        ViewpointTagSaveResult viewpointTagSaveResult =new ViewpointTagSaveResult();
        return VOUtil.setSuccessResult(viewpointTagSaveResult);
    }

    private void setViewpointTopicAndTagInfo(List<ViewpointRecomListData> viewpointRecomListDatas) {
        //添加标签名称
        ViewpointTag viewpointTag = new ViewpointTag();
        List<ViewpointTag> viewpointTags = viewpointTagService.query(viewpointTag);
        Map<Long,String> tagMap = new HashMap<>();
        for (ViewpointTag tmp :viewpointTags) {
            if (tmp.getViewpoint_tag_id()!=null) {
                tagMap.put(tmp.getViewpoint_tag_id(), tmp.getTag_name());
            }
        }
        //添加话题名称
        List<Long> topicIds = new ArrayList<>();
        for (ViewpointRecomListData viewpointRecomListData: viewpointRecomListDatas){
            topicIds.add(viewpointRecomListData.getViewpoint_topic_id());
        }
        if (topicIds==null || topicIds.size()<1){
            return;
        }
        List<ViewpointTopic> viewpointTopicList = viewpointTopicService.getTopicByIds(topicIds);
        Map<Long,String> topicMap = new HashMap();
        for (ViewpointTopic viewpointTopic :viewpointTopicList) {
            if (viewpointTopic.getViewpoint_topic_id()!=null) {
                topicMap.put(viewpointTopic.getViewpoint_topic_id(), viewpointTopic.getTopic_name());
            }
        }
        for (ViewpointRecomListData viewpointRecomListData: viewpointRecomListDatas){
            viewpointRecomListData.setViewpoint_topic_name(topicMap.get(viewpointRecomListData.getViewpoint_topic_id()));
            viewpointRecomListData.setViewpoint_tag_name(tagMap.get(viewpointRecomListData.getViewpoint_tag_id()));
        }
    }

    private void setLeaderViewpointTopicAndTagInfo(List<ViewpointListData> viewpointRecomListDatas) {
        //添加标签名称
        ViewpointTag viewpointTag = new ViewpointTag();
        List<ViewpointTag> viewpointTags = viewpointTagService.query(viewpointTag);
        Map<Long,String> tagMap = new HashMap<>();
        for (ViewpointTag tmp :viewpointTags) {
            if (tmp.getViewpoint_tag_id()!=null) {
                tagMap.put(tmp.getViewpoint_tag_id(), tmp.getTag_name());
            }
        }
        //添加话题名称
        List<Long> topicIds = new ArrayList<>();
        for (ViewpointListData viewpointListData: viewpointRecomListDatas){
            topicIds.add(viewpointListData.getViewpoint_topic_id());
        }
        if (topicIds==null || topicIds.size()<1){
            return;
        }
        List<ViewpointTopic> viewpointTopicList = viewpointTopicService.getTopicByIds(topicIds);
        Map<Long,String> topicMap = new HashMap();
        for (ViewpointTopic viewpointTopic :viewpointTopicList) {
            if (viewpointTopic.getViewpoint_topic_id()!=null) {
                topicMap.put(viewpointTopic.getViewpoint_topic_id(), viewpointTopic.getTopic_name());
            }
        }
        for (ViewpointListData viewpointListData: viewpointRecomListDatas){
            viewpointListData.setViewpoint_topic_name(topicMap.get(viewpointListData.getViewpoint_topic_id()));
            viewpointListData.setViewpoint_tag_name(tagMap.get(viewpointListData.getViewpoint_tag_id()));
        }
    }

    public ViewpointRecomList funcViewpointSearch(ViewpointQueryForm form) {
        //添加筛选非关闭观点
        form.setClose_status(CloseStatus.NORMAL.getVal());
        List<ViewpointMsg> viewpointMsgs = viewpointMsgService.queryByKeyWord(form);

        //观点datalist组装
        List<ViewpointRecomListData> viewpointRecomListDatas = toFansViewpointList(viewpointMsgs);
        //投顾信息插入
        setLeaderInfo(viewpointRecomListDatas,form.getUser_token());
        //添加话题信息
        setViewpointTopicAndTagInfo(viewpointRecomListDatas);
        //插入关注信息
        if (form.getUser()!=null){
            setUpvoteInfo(viewpointRecomListDatas,form.getUser().getUser_id());
        }else {
            setUpvoteInfo(viewpointRecomListDatas,null);
        }
        //result组装
        ViewpointRecomList viewpointRecomList = new ViewpointRecomList();
        viewpointRecomList.setData_list(viewpointRecomListDatas);
        viewpointRecomList.setPage_no(form.getPage_no());
        viewpointRecomList.setTotal_item_num(viewpointMsgService.countByKeyword(form));
        viewpointRecomList.setPage_size(form.getRequest_num());
        return VOUtil.setSuccessResult(viewpointRecomList);

    }
}
